<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>
<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import { getDateStr, getDaysPriorDateStr, getMonday, getDaysLateDateStr } from '@/utils/commonUtils';
import { listOrderTjAmount } from "@/api/business/order";
import { listMarketTjAmount } from "@/api/business/market";
export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null,
      payAmount: 0,
      marketAmount: 0,
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initValue()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initValue() {
      let date1 = getDaysPriorDateStr(new Date(),6);
      let date7 = getDateStr(new Date());
      /*let Monday = getMonday();
      let Sunday = getDaysLateDateStr(new Date(Monday),6);*/
      let orderParam = {createAtstart: date1, createAtend: date7};
      let marketParam = {createAtstart: date1, createAtend: date7};
      listOrderTjAmount(orderParam).then(response => {
        response.data.forEach(item => {
          this.payAmount = this.payAmount+item.payAmount;
        });
      });
      listMarketTjAmount(marketParam).then(response => {
        response.data.forEach(item => {
          this.marketAmount = this.marketAmount+item.marketAmount;
        });
      });
      setTimeout(this.initChart, 4000);
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption({
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          left: 'center',
          bottom: '10',
          data: ['销售金额', '分销金额']
        },
        series: [
          {
            name: '周报',
            type: 'pie',
            roseType: 'radius',
            radius: [15, 95],
            center: ['50%', '50%'],
            data: [
              { value: this.payAmount, name: '销售金额' },
              { value: this.marketAmount, name: '分销金额' }              
            ],
            animationEasing: 'cubicInOut',
            animationDuration: 2600
          }
        ]
      })
    }
  }
}
</script>